<template>
  <div class="shop">
    <ul class="nav">
      <li>
        全部(20)
        <span></span>
      </li>
      <li>
        降价(4)
        <span></span>
      </li>
      <li>
        常卖(2)
        <span></span>
      </li>
      <li>
        分类
        <img src="../../../assets/img/img/商品列表 (1).png" alt />
        <span></span>
      </li>
    </ul>
    <div class="content">
      <div>
        <div v-for="item in arr" :key="item.id">
          <input type="checkbox" name id v-model="item.tag" @change="check(item)" />
          <img :src="$pre+item.img" alt />
          <ul>
            <li>{{item.goodsname}}</li>
            <li>
              <div class="fl">
                <span>￥{{item.price}}</span>
                <span>￥1099</span>
              </div>
              <div class="fr">
                <img src="../../../assets/img/img/购物车 (6).png" alt class="fr" @click="add(item.id)" />
                <i class="fr">{{item.num}}</i>
                <img src="../../../assets/img/img/购物车 (5).png" alt class="fr" @click="sub(item)" />
              </div>
            </li>
            <li>
              <van-button type="danger" @click="del(item.id)">删除</van-button>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="bottom1 clearfix">
      <div class="fl">
        <input type="checkbox" @change="allcheck" v-model="allchecks" />
        <span>全选</span>
      </div>
      <ul class="fr clearfix">
        <li class="fr">
          <input type="button" value="结算" @click="goorder" />
        </li>
        <li class="fr">
          <p>￥{{allprice}}</p>
          <p>已优惠￥200元</p>
        </li>
        <li class="fr">
          <p>合计</p>
          <p>已免运费</p>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import {
  reqShopList,
  reqShopEdit,
  reqShopDel,
  reqOrderAdd
} from "../../../http/api.js";
export default {
  data() {
    return {
      allchecks: false,
      arr: [],
      isChange: true
    };
  },
  mounted() {
    this.res();
  },
  computed: {
    allprice() {
      let sum = this.arr.reduce((val, item) => {
        if (item.tag) {
          return val + item.price * item.num;
        } else {
          return val;
        }
      }, 0);
      return sum;
    }
  },
  methods: {
    res() {
      let checkedarr = [...this.arr];
      reqShopList({
        uid: JSON.parse(localStorage.getItem("user")).uid,
        authorization: JSON.parse(localStorage.getItem("user")).token
      }).then(res => {
        res.data.list.forEach(item => {
          item.tag = checkedarr.find(i => i.id == item.id)
            ? checkedarr.find(i => i.id == item.id).tag
            : false;
        });
        this.arr = res.data.list;
        if (res.data.code == 200) {
          this.isChange = true;
          this.allchecks = this.arr.every(item => {
            return item.tag;
          });
        }
      });
    },
    del(id) {
      reqShopDel({
        id: id,
        authorization: JSON.parse(localStorage.getItem("user")).token
      }).then(res => {
        this.res();
      });
    },
    allcheck() {
      this.arr.forEach(item => {
        item.tag = this.allchecks;
      });
    },
    check(item) {
      this.allchecks = this.arr.every(item => {
        return item.tag;
      });
    },
    add(id) {
      reqShopEdit({
        id: id,
        type: 2,
        authorization: JSON.parse(localStorage.getItem("user")).token
      }).then(res => {
        this.res();
        // this.arr.find(i=>i.id==id).num++
      });
    },
    sub(item) {
      if (this.isChange) {
        this.isChange = false;
        if (item.num > 1) {
          reqShopEdit({
            id: item.id,
            type: 1,
            authorization: JSON.parse(localStorage.getItem("user")).token
          }).then(res => {
            if (res.data.code == 200) {
              this.res();
            }
            // this.arr.find(i=>i.id==item.id).num--
          });
        }
      }
    },
    goorder() {
      let str = this.arr.reduce((val, item) => {
        return item.tag ? (val += item.id + ",") : val;
      }, "");
      let json = {
        countmoney: this.allprice,
        countnumber: this.arr.reduce((val, item) => {
          return item.num ? (val += item.num) : val;
        }, 0),
        idstr: str.slice(0, str.length - 1)
      };
      localStorage.setItem("order", JSON.stringify(json));
      if (
        this.arr.some(item => {
          return item.tag;
        })
      ) {
        this.$router.push("/order");
      }
    }
  }
};
</script>

<style scoped>
.nav {
  font-size: 0.26rem;
  height: 0.8rem;
  color: white;
  background: linear-gradient(to bottom, #ff7a68, #ff8a7f);
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  align-items: center;
  line-height: 0.75rem;
  /* position: relative; */
  overflow: hidden;
  box-shadow: 0px 4px 4px #ffd7d0;
}
.nav li:hover span {
  width: 0.4rem;
  height: 0.05rem;
  border-radius: 1px;
  background: white;
  display: block;
  margin: 0 auto;
}
.nav li:hover {
  font-weight: bolder;
  font-size: 0.3rem;
}
.nav li:nth-of-type(4) img {
  width: 0.13rem;
  height: 0.26rem;
  float: right;
  margin: 12px 0 0 6px;
}
.content {
  background: #f2f2f2;
  overflow: hidden;
}
.content > div {
  background: white;
  padding: 0 0.2rem;
  margin: 0.2rem 0;
  overflow: hidden;
}
.content > div > div {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0.4rem 0;
}
.content > div > div img {
  width: 2.16rem;
  height: 2.16rem;
}
.content > div > div li:nth-of-type(1) {
  font-size: 0.3rem;
}
.content > div > div li:nth-of-type(2) {
  font-size: 0.24rem;
  color: gray;
  line-height: 0.4rem;
}
.content > div > div select {
  margin-top: 0.2rem;
  font-size: 0.16rem;
  background: #f2f2f2;
  border: 0;
}
.content > div > div .fl span:nth-of-type(1) {
  font-size: 0.3rem;
  font-weight: bolder;
  color: #ff775b;
}
.content > div > div span:nth-of-type(2) {
  font-size: 0.12rem;
  color: gray;
  text-decoration: line-through;
}
.content > div > div .fl {
  padding-top: 0.06rem;
}
.content > div > div .fr img {
  width: 0.51rem;
  height: 0.4rem;
  background: #f2f2f2;
  border: 0;
}
.content > div > div .fr i {
  width: 0.6rem;
  text-align: center;
}
.bottom1 {
  padding-left: 0.2rem;
  height: 1rem;
  margin-bottom: 100px;
}
.bottom1 .fl input {
  margin-bottom: 0.1rem;
}
.bottom1 .fl span {
  line-height: 0.72rem;
  padding: 0 0.2rem;
  color: black;
  font-size: 0.3rem;
  font-weight: bolder;
}
.bottom1 ul input {
  height: 1rem;
  width: 2.2rem;
  color: #fff;
  background: #ff6040;
}
.bottom1 ul li {
  margin-left: 0.2rem;
}
.bottom1 ul li:nth-of-type(2) {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: flex-end;
  height: 0.7rem;
  margin-top: 0.15rem;
}
.bottom1 ul li:nth-of-type(3) {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  height: 0.7rem;
  margin-top: 0.15rem;
}
.bottom1 ul li:nth-of-type(2) p:nth-of-type(1) {
  color: #ff6040;
  font-size: 0.3rem;
  font-weight: bolder;
}
.bottom1 ul li:nth-of-type(3) p:nth-of-type(1) {
  color: black;
  font-size: 0.3rem;
  font-weight: bolder;
}
.bottom1 ul p:nth-of-type(2) {
  font-size: 0.12rem;
  color: gray;
}
</style>